<!DOCTYPE html>
<html lang="zh-cn">

<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>注册</title>

  <!-- Bootstrap -->
  <link href="./lib/css/bootstrap.min.css" rel="stylesheet" />

  <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  <!--[if lt IE 9]>
      <script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  <style>
    .login-panel {
      margin-top: 200px;
    }

    .vCode {
      height: 36px;
    }
  </style>
</head>

<body>
  <div class="container">
    <div class="row">
      <div class="col-md-6 col-md-offset-3">
        <div class="panel panel-primary login-panel">
          <div class="panel-heading">
            <h3 class="panel-title">奇思妙想后台管理系统注册</h3>
          </div>
          <div class="panel-body">
            <form class="form-horizontal" method="post" action="">
              <div class="form-group">
                <label for="username" class="col-sm-2 control-label">用户名</label>
                <div class="col-sm-10">
                  <input type="text" name="username" value="user" class="form-control" id="username"
                    placeholder="请输入用户名" />
                </div>
              </div>
              <div class="form-group">
                <label for="password" class="col-sm-2 control-label">密码</label>
                <div class="col-sm-10">
                  <input type="password" name="password" value="" class="form-control" id="password"
                    placeholder="请输入密码" />
                </div>
              </div>

              <div class="form-group">
                <div class="col-sm-offset-2 col-sm-4">
                  <button type="button" class="btn btn-success register">
                    注册
                  </button>
                  <a href="./login.html" class="btn btn-danger">返回</a>
                </div>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
  </div>
</body>

</html>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="./lib/js/jquery-1.12.4.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="./lib/js/bootstrap.min.js"></script>

<script src="./lib/md5/md5.min.js"></script>
<script>
  $(function () {
    $(".register").click(function () {
      // 通过正则判断用户输入的用户名是否是英文单词
      const regUserName = /^[a-z]+$/i;
      const userName = $("#username").val();

      // 判断用户名是否符合要求
      if (regUserName.test(userName)) {
        console.log("符合用户名要求");
      } else {
        console.log("不符合要求");

        // 如果没有匹配到 那么  就结束函数体的执行
        return;
      }

      // 获取密码
      let userPassword = $("#password").val();

      // 调用加密方法
      userPassword = md5(userPassword, "md5");

      // 发送请求
      $.ajax({
        type: "post",
        url: "/register",
        data: {
          userName: $("#username").val(),
          userPassword
        },
        success(data) {
          console.log(data);
          if (data.status == 400) {
            alert(data.msg);
            location.href = "./login.html";
          }
        }
      });
    });
  });

</script>